package com.crk.yiyuanguahao.service.impl;

import com.crk.yiyuanguahao.dao.DoctorAppointmentDao;
import com.crk.yiyuanguahao.dao.DoctorDao;
import com.crk.yiyuanguahao.dao.UserDao;
import com.crk.yiyuanguahao.entity.DoctorAppointment;
import com.crk.yiyuanguahao.entity.DoctorAppointmentDto;
import com.crk.yiyuanguahao.entity.User;
import com.crk.yiyuanguahao.service.DoctorAppointmentService;
import com.crk.yiyuanguahao.util.Page;
import com.crk.yiyuanguahao.util.ResponseEnum;
import com.crk.yiyuanguahao.util.ResponseResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @description:
 * @date 2020/3/26
 */
@Service
public class DoctorAppointmentServiceImpl implements DoctorAppointmentService {

    @Autowired
    DoctorAppointmentDao doctorAppointmentDao;
    @Autowired
    UserDao userDao;
    @Autowired
    DoctorDao doctorDao;

    private final static SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * 保存预约信息
     *
     * @param doctorAppointment 预约信息
     * @return
     */
    @Override
    public ResponseResult saveDoctorAppointment(DoctorAppointment doctorAppointment) {
        if (null != doctorAppointment){
            int result = 0;
            if (doctorAppointment.getId() == null){
                doctorAppointment.setCreateTime(new Date());
                doctorAppointment.setStatus(1);
                result = doctorAppointmentDao.insert(doctorAppointment);
            }else{
                result = doctorAppointmentDao.updateByPrimaryKey(doctorAppointment);
            }

            if (result > 0){
                return ResponseResult.success(doctorAppointment);
            }else{
                return ResponseResult.fail();
            }
        }else {
            return ResponseResult.fail();
        }
    }

    @Override
    public ResponseResult createDoctorAppointment(DoctorAppointment doctorAppointment) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        doctorAppointment.setStatus(1);
        doctorAppointment.setPatientId(user.getId());
        doctorAppointment.setCreateTime(new Date());
        doctorAppointmentDao.insert(doctorAppointment);
        return ResponseResult.success();
    }

    /**
     * 删除预约信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteDoctorAppointment(Integer id) {
        int i = doctorAppointmentDao.deleteByPrimaryKey(id);
        if (i > 0){
            return ResponseResult.success();
        }else{
            return ResponseResult.fail();
        }
    }

    /**
     * 查询预约信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getDoctorAppointment(Integer id) {
        DoctorAppointment doctorAppointment = doctorAppointmentDao.selectByPrimaryKey(id);
        DoctorAppointmentDto doctorAppointmentDto = new DoctorAppointmentDto();
        doctorAppointmentDto.setAppointmentTimeStr(SIMPLE_DATE_FORMAT.format(doctorAppointment.getAppointmentTime()));
        doctorAppointmentDto.setId(doctorAppointment.getId());
        doctorAppointmentDto.setDoctorCode(doctorAppointment.getDoctorCode());
        doctorAppointmentDto.setPatientId(doctorAppointment.getPatientId());
        doctorAppointmentDto.setAppointmentDetailTime(doctorAppointment.getAppointmentDetailTime());
        doctorAppointmentDto.setStatus(doctorAppointment.getStatus());
        return ResponseResult.success(doctorAppointmentDto);
    }

    /**
     * 查询预约信息列表
     *
     * @param doctorCode 医生编号
     * @param patientId     用户ID
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @param pageInfo   分页
     * @return
     */
    @Override
    public ResponseResult queryDoctorAppointmentPage(String doctorCode, Integer patientId, String startTime, String endTime, Page pageInfo) {
        Integer count = doctorAppointmentDao.countByCondition(doctorCode, patientId,startTime,endTime);
        pageInfo.setAllRow(count);
        List<DoctorAppointment> doctorList = doctorAppointmentDao.selectByCondition(doctorCode, patientId,startTime,endTime, pageInfo.getStart(), pageInfo.getSize());
        List<DoctorAppointmentDto> doctorAppointmentDtos = new ArrayList<>();
        for (DoctorAppointment doctorAppointment : doctorList) {
            DoctorAppointmentDto doctorAppointmentDto = new DoctorAppointmentDto();
            doctorAppointmentDto.setId(doctorAppointment.getId());
            doctorAppointmentDto.setCreateTime(doctorAppointment.getCreateTime());
            doctorAppointmentDto.setDoctorCode(doctorAppointment.getDoctorCode());
            doctorAppointmentDto.setStatus(doctorAppointment.getStatus());
            doctorAppointmentDto.setAppointmentTime(doctorAppointment.getAppointmentTime());
            doctorAppointmentDto.setAppointmentDetailTime(doctorAppointment.getAppointmentDetailTime());

            doctorAppointmentDto.setPatientName(userDao.selectByPrimaryKey(doctorAppointment.getPatientId()).getUserName());
            doctorAppointmentDto.setDoctorName(doctorDao.selectDoctorByCode(doctorAppointment.getDoctorCode()).getDoctorName());
            doctorAppointmentDtos.add(doctorAppointmentDto);
        }
        return ResponseResult.success(doctorAppointmentDtos);
    }

    /**
     * 查询预约信息列表
     *
     * @param doctorCode 医生编号
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @param pageInfo   分页
     * @return
     */
    @Override
    public ResponseResult queryMyDoctorAppointmentPage(String doctorCode, String startTime, String endTime, Page pageInfo) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        int patientId = user.getId();
        Integer count = doctorAppointmentDao.countByCondition(doctorCode, patientId,startTime,endTime);
        pageInfo.setAllRow(count);
        List<DoctorAppointment> doctorList = doctorAppointmentDao.selectByCondition(doctorCode, patientId,startTime,endTime, pageInfo.getStart(), pageInfo.getSize());
        List<DoctorAppointmentDto> doctorAppointmentDtos = new ArrayList<>();
        for (DoctorAppointment doctorAppointment : doctorList) {
            DoctorAppointmentDto doctorAppointmentDto = new DoctorAppointmentDto();
            doctorAppointmentDto.setId(doctorAppointment.getId());
            doctorAppointmentDto.setCreateTime(doctorAppointment.getCreateTime());
            doctorAppointmentDto.setDoctorCode(doctorAppointment.getDoctorCode());
            doctorAppointmentDto.setStatus(doctorAppointment.getStatus());
            doctorAppointmentDto.setAppointmentTime(doctorAppointment.getAppointmentTime());
            doctorAppointmentDto.setAppointmentDetailTime(doctorAppointment.getAppointmentDetailTime());

            doctorAppointmentDto.setPatientName(userDao.selectByPrimaryKey(doctorAppointment.getPatientId()).getUserName());
            doctorAppointmentDto.setDoctorName(doctorDao.selectDoctorByCode(doctorAppointment.getDoctorCode()).getDoctorName());
            doctorAppointmentDtos.add(doctorAppointmentDto);
        }
        return ResponseResult.success(doctorAppointmentDtos);
    }

    /**
     * 查询预约信息列表
     *
     * @param startTime  开始时间
     * @param endTime    结束时间
     * @param pageInfo   分页
     * @return
     */
    @Override
    public ResponseResult querySelfDoctorAppointmentPage(String appointmentTime, String appointmentDetailTime, Page pageInfo) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        String doctorCode = user.getDoctorCode();
        Integer count = doctorAppointmentDao.countByCondition(doctorCode, null,appointmentTime,appointmentDetailTime);
        pageInfo.setAllRow(count);
        List<DoctorAppointment> doctorList = doctorAppointmentDao.selectByCondition(doctorCode, null,appointmentTime,appointmentDetailTime, pageInfo.getStart(), pageInfo.getSize());
        List<DoctorAppointmentDto> doctorAppointmentDtos = new ArrayList<>();
        for (DoctorAppointment doctorAppointment : doctorList) {
            DoctorAppointmentDto doctorAppointmentDto = new DoctorAppointmentDto();
            doctorAppointmentDto.setId(doctorAppointment.getId());
            doctorAppointmentDto.setCreateTime(doctorAppointment.getCreateTime());
            doctorAppointmentDto.setDoctorCode(doctorAppointment.getDoctorCode());
            doctorAppointmentDto.setStatus(doctorAppointment.getStatus());
            doctorAppointmentDto.setAppointmentTime(doctorAppointment.getAppointmentTime());
            doctorAppointmentDto.setAppointmentDetailTime(doctorAppointment.getAppointmentDetailTime());

            doctorAppointmentDto.setPatientName(userDao.selectByPrimaryKey(doctorAppointment.getPatientId()).getUserName());
            doctorAppointmentDto.setDoctorName(doctorDao.selectDoctorByCode(doctorAppointment.getDoctorCode()).getDoctorName());
            doctorAppointmentDtos.add(doctorAppointmentDto);
        }
        return ResponseResult.success(doctorAppointmentDtos);
    }

    @Override
    public ResponseResult queryLastAppointment() {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        Integer userType = user.getUserType();
        List<DoctorAppointment> doctorAppointments = new ArrayList<>();
        if (userType == 1){
            doctorAppointments = doctorAppointmentDao.queryLastAppointment(null,user.getDoctorCode());
        }else{
            doctorAppointments = doctorAppointmentDao.queryLastAppointment(user.getId(),null);
        }
        List<DoctorAppointmentDto> doctorAppointmentDtos = new ArrayList<>();
        for (DoctorAppointment doctorAppointment : doctorAppointments) {
            DoctorAppointmentDto doctorAppointmentDto = new DoctorAppointmentDto();
            doctorAppointmentDto.setId(doctorAppointment.getId());
            doctorAppointmentDto.setCreateTime(doctorAppointment.getCreateTime());
            doctorAppointmentDto.setDoctorCode(doctorAppointment.getDoctorCode());
            doctorAppointmentDto.setStatus(doctorAppointment.getStatus());
            doctorAppointmentDto.setAppointmentTime(doctorAppointment.getAppointmentTime());
            doctorAppointmentDto.setAppointmentDetailTime(doctorAppointment.getAppointmentDetailTime());

            doctorAppointmentDto.setPatientName(userDao.selectByPrimaryKey(doctorAppointment.getPatientId()).getUserName());
            doctorAppointmentDto.setDoctorName(doctorDao.selectDoctorByCode(doctorAppointment.getDoctorCode()).getDoctorName());
            doctorAppointmentDtos.add(doctorAppointmentDto);
        }
        return ResponseResult.success(doctorAppointmentDtos);

    }
}
